package com.hzhr.extdevice.util;

import java.util.ArrayList;
import java.util.List;

public class StrUtil
{
    public static String replaceLast(String text, String regex, String replacement) { return text.replaceFirst("(?s)" + regex + "(?!.*?" + regex + ")", replacement); }


    public static String obj2str(Object object) {
        if (object == null) return null;
        return object.toString();
    }





    public static String FillZeroForLeft(String str, int size) {
        int strSize = str.length();
        if (strSize < size) {
            int zeroCount = size - strSize;
            for (int i = 0; i < zeroCount; i++) {
                str = "0" + str;
            }
        }
        return str;
    }





    public static String removeZeroForLeft(String str) {
        int strSize = str.length();
        String result = "";
        for (int i = 0; i < strSize; i++) {
            char c = str.charAt(i);
            if (c != '0') {
                result = String.valueOf(result) + str.substring(i);
                break;
            }
        }
        return result;
    }




    public static String firstCharToUppserCase(String str) {
        StringBuilder sb = new StringBuilder();
        char[] cs = str.toCharArray();

        for (int i = 0; i < cs.length; i++) {
            char c = cs[i];
            if (i == 0) {
                sb.append(Character.toUpperCase(c));
            } else {
                sb.append(c);
            }
        }

        return sb.toString();
    }




    public static String firstCharToLowerCase(String str) {
        StringBuilder sb = new StringBuilder();
        char[] cs = str.toCharArray();

        for (int i = 0; i < cs.length; i++) {
            char c = cs[i];
            if (i == 0) {
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }

        return sb.toString();
    }












    public static String strToLowerCase(String str) {
        StringBuilder sb = new StringBuilder();
        char[] cs = str.toCharArray();

        for (int i = 0; i < cs.length; i++) {
            char c = cs[i];
            if (Character.isUpperCase(c)) {
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }

        return sb.toString();
    }




    public static String splitHumpStr(String str, String regex) {
        StringBuilder sb = new StringBuilder();
        char[] cs = str.toCharArray();

        for (int i = 0; i < cs.length; i++) {
            char c = cs[i];
            if (Character.isUpperCase(c)) {
                sb.append(String.valueOf(regex) + Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }

        return sb.toString();
    }




    public static String zhuanTuoFeng(String str, String regex) {
        str = str.toLowerCase();
        String[] split = str.split(regex);
        String result = "";
        for (int i = 0; i < split.length; i++) {
            String item = split[i];
            if (i == 0) { result = String.valueOf(result) + item; }
            else { result = String.valueOf(result) + firstCharToUppserCase(item); }

        }  return result;
    }
}
